package com.sf.asp.controller;


import cn.hutool.core.map.MapUtil;
import com.sf.asp.entity.dto.*;
import com.sf.asp.entity.vo.ResponseInfo;
import com.sf.asp.service.PermissionService;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.jws.Oneway;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jins
 * @since 2021-11-24
 */
@Controller
public class PermissionController {

    private final PermissionService permissionService;

    public PermissionController(PermissionService permissionService){
        this.permissionService = permissionService;
    }



    @GetMapping("/permission")
    public String menuPage(){
        return "manage/permission/list";
    }

    @GetMapping("/permission/save")
    public String menuSavePage(){
        return "manage/permission/save";
    }

    @GetMapping("/permission/edit/{menuId}")
    public String menuEditPage(@PathVariable Integer menuId, ModelMap map) {

        permissionService.getById(menuId);
        map.addAttribute("menu",permissionService.getById(menuId));
        return "manage/permission/edit";
    }

    @GetMapping("/permission/tree/{roleId}")
    public String permissionTree(@PathVariable Integer roleId, ModelMap map) {
        map.addAttribute("roleId",roleId);
        return "manage/permission/tree";
    }



    @GetMapping(value = "/permission/menu")
    @ResponseBody
    public List<MenuDTO> getUserMenu(){
        return permissionService.getUserMenu("");
    }


    @GetMapping("/menus")
    @ResponseBody
    public ResponseInfo menus(){
        return ResponseInfo.ok(permissionService.listMenu());
    }


    @GetMapping("/menus/parents")
    @ResponseBody
    public Map<String, Object> menuParents(){
        Map<String,Object> map = new HashMap<>();
        map.put("data",permissionService.menuParents());
        map.put("status",MapUtil.of("code",200));
        return map;
    }

    @GetMapping(value = "/permission/{roleId}")
    @ResponseBody
    public Map<String, Object> getPermissionByRole(@PathVariable Integer roleId){
        Map<String,Object> map = new HashMap<>();
        map.put("data",permissionService.getPermissionByRole(roleId));
        map.put("status",MapUtil.of("code",200));
        return map;
    }


    @PostMapping("/menu/save")
    @ResponseBody
    public ResponseInfo saveMenu(@RequestBody MenuSaveOrUpdateDTO menu){

        permissionService.saveMenu(menu);
        return ResponseInfo.ok();
    }

    @PostMapping("/menu/delete")
    @ResponseBody
    public ResponseInfo deleteMenu(@RequestBody DeleteDTO delete){
        permissionService.deleteMenu(delete);
        return ResponseInfo.ok();
    }

    @PostMapping("/menu/edit")
    @ResponseBody
    public ResponseInfo editMenu(@RequestBody MenuSaveOrUpdateDTO menu){
        permissionService.editMenu(menu);
        return ResponseInfo.ok();
    }



}
